import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { RejectReason } from './rejectReason.entity';
import { EnumRejectReasonType } from './rejectReason.enum';

@Injectable()
export class RejectReasonService {
  constructor(
    @InjectRepository(RejectReason)
    private repository: Repository<RejectReason>,
  ) {}

  findAll(): Promise<RejectReason[]> {
    return this.repository.find();
  }

  findOne(id: any): Promise<RejectReason> {
    return this.repository.findOne(id);
  }

  async remove(id: number): Promise<void> {
    await this.repository.delete(id);
  }

  async create(cats: RejectReason): Promise<void> {
    await this.repository.save(cats);
  }

  findAll_ByType(typeStr: string) : Promise<RejectReason[]> | [] {
    let type
    switch(typeStr){
      case 'question':
        type = EnumRejectReasonType.question
        break;
      case 'original':
        type = EnumRejectReasonType.original
        break;
      case 'answer':
        type = EnumRejectReasonType.answer
        break;
      case 'refund':
        type = EnumRejectReasonType.refund
        break;
    }
    if(type === undefined){
      return []
    }
    return this.repository.find({where:{type: type}})
  }
}
